<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <!-- import CSS -->
  <link rel="stylesheet" href="./css/element-ui.css">
  <!-- import Vue before Element -->
  <script src="./js/vue.js"></script>
  <!-- import JavaScript -->
  <script src="./js/element-ui.js"></script>
  <script src="./js/axios.js"></script>
  <style>
    #app {
      padding: 20px;
    }

    .login {
      display: flex;
      justify-items: center;
      align-items: center;
    }

    .el-form-item__label {
      width: 80px;
    }

    .el-form-item__content {
      display: flex;
    }
  </style>
</head>

<body>
  <div id="app">
    <div>
      <el-select v-model="type" placeholder="请选择">
        <el-option label="审核成功" value="toAppSuccess"></el-option>
        <el-option label="审核失败" value="toAppFailure"></el-option>
        <el-option label="等待审核" value="toAppAudit"></el-option>
        <el-option label="资料不全" value="incomplete"></el-option>
        <el-option label="爬取图片" value="images"></el-option>
      </el-select>
      <el-button type="primary" @click="start">开始</el-button>
      <span>总数：{{ info.total }}</span>
      <span>进度：{{ info.processed }}</span>
    </div>
    <el-progress :percentage="processed"></el-progress>
    <el-dialog title="登录" :visible="info.status === 'logout'" width="450px" :show-close="false">
      <el-form :model="form">
        <el-form-item label="用户名">
          <el-input v-model="form.username" placeholder="微租车登录用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" placeholder="微租车登录密码"></el-input>
        </el-form-item>
        <el-form-item label="验证码">
          <el-input v-model="form.code" placeholder="微租车登录密码" style="width: 230px;"></el-input>
          <img :src="'/verifyCode?=' + random" alt="" style="height: 40px; width: 100px;" @click="verifyCode">
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button type="primary" @click="login" :loading="loading">登录</el-button>
      </span>
    </el-dialog>
  </div>
</body>
<script>
  new Vue({
    el: '#app',
    data: function () {
      return {
        dialogVisible: true,
        loading: false,
        random: Math.random(),
        type: 'toAppSuccess',
        form: {
          username: 'wufu_15960278116',
          password: 'Wf135790'
        },
        info: {
          processed: 0,
          total: 100
        }
      }
    },
    computed: {
      processed() {
        if (this.info.total === 0) {
          return 0
        }
        return (this.info.processed * 1.0 / this.info.total * 100).toFixed(1)
      }
    },
    created() {
      let ws = new WebSocket('ws://localhost:3001')
      // 响应收到的消息:
      ws.onmessage = (message) => {
        this.info = JSON.parse(message.data)
      }
    },
    methods: {
      verifyCode() {
        this.random = Math.random()
      },
      login() {
        this.loading = true
        axios.post('/login', {
          params: this.form
        }).then(response => {
          if (response.data.success) {
            this.dialogVisible = false
          } else {
            ELEMENT.Message.error(response.data.errors.join(','))
          }
        }).catch(error => {
          ELEMENT.Message.error(error)
          this.verifyCode()
        }).finally(() => {
          this.loading = false
        })
      },
      start() {
        axios.get('/task', {
          params: { type: this.type }
        })
      }
    }
  })
</script>

</html>